package com.control.qct.businesssteward.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.ChangeGuideAdapter;
import com.control.qct.businesssteward.adapter.OpenOrderAdapter;
import com.control.qct.businesssteward.adapter.SelectGoodsAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.model.DoPayGoodsModel;
import com.control.qct.businesssteward.model.DoPayPromotionGoodsModel;
import com.control.qct.businesssteward.model.GuideModel;
import com.control.qct.businesssteward.model.MemberInquireModel;
import com.control.qct.businesssteward.model.OpenOrderModel;
import com.control.qct.businesssteward.fragment.MemberInquireFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.RemoveGoodsItemsModel;
import com.control.qct.businesssteward.utils.CustomCodeUtils;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.DialogUtil;
import com.control.qct.businesssteward.utils.GlideUtil;
import com.control.qct.businesssteward.utils.GsonUtil;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 开单
 * Created by Administrator on 2017/11/24 0024.
 */

public class OpenOrderActivity extends BaseActivity implements MemberInquireFragment.ICallbackMemberListener,
        CustomCodeUtils.ICallbackListener {

    @BindView(R.id.iv_return)
    ImageView mIvReturn;//返回
    @BindView(R.id.tv_member)
    TextView mTvMember;//会员
    @BindView(R.id.tv_editor)
    TextView mTvEditor;//编辑
    @BindView(R.id.ib_retail)
    ImageButton mIbRetail;//零售图标--点击切换批发图标
    @BindView(R.id.et_barcode)
    EditText mEtBarcode;//自己输入的条形码
    @BindView(R.id.iv_scan)
    ImageView mIvScan;//扫描
    @BindView(R.id.iv_sure)
    ImageView mIvSure;//确认
    @BindView(R.id.tv_collect)
    TextView mTvCollect;//应收
    @BindView(R.id.tv_discounts)
    TextView mTvDiscounts;//优惠
    @BindView(R.id.tv_num)
    TextView mTvNum;//件数
    @BindView(R.id.tv_member_name)
    TextView mTvMemberName;//会员名
    @BindView(R.id.tv_member_phone)
    TextView mTvMemberPhone;//会员的电话
    @BindView(R.id.iv_delete)
    ImageView mIvDelete;//删除会员
    @BindView(R.id.ll_member)
    LinearLayout mLlMember;//会员显示行
    @BindView(R.id.tv_guide_num)
    TextView mTvGuideNum;//导购员的数量
    @BindView(R.id.rv_goods)
    RecyclerView mRvGoods;//加入购物车的商品列表
    @BindView(R.id.tv_all_discounts)
    TextView mTvAllDiscounts;//整单优惠
    @BindView(R.id.tv_goto_account)
    TextView mTvGotoAccount;//结算
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;//侧滑菜单布局
    @BindView(R.id.dl_menu)
    public DrawerLayout mDlMenu;//侧滑菜单控件
    @BindView(R.id.cb_all_checked)
    CheckBox mCbAllChecked;//全选
    //    @BindView(R.id.tv_guide)
//    TextView mTvGuide;//导购员
    @BindView(R.id.tv_delete)
    TextView mTvDelete;//删除
    @BindView(R.id.rl_all_checked)
    RelativeLayout mRlAllChecked;//全选根布局
    @BindView(R.id.ll_bottom)
    LinearLayout mLlBottom;//全选根布局

    private List<OpenOrderModel.CarItemsBean.ItemsBean> mOpenOrderModelItemList;//购物车清单列表
    private OpenOrderAdapter mOpenOrderAdapter;
    private boolean isEdit;
    private List<Boolean> isAllChecked = new ArrayList<>();

    //删除会员弹窗
    private Dialog mDeleteDialog;
    private View mDeleteView;
    //改价弹窗
    private View mChangePriceView;
    private Dialog mChangePriceDialog;
    //导购员弹窗
    private View mChangeGuideView;
    private Dialog mChangeGuideDialog;
    private List<GuideModel.DatasBean> mGuideNameList = new ArrayList<>();
    private Map<String, String> mParams;
    private ChangeGuideAdapter mChangeGuideAdapter;
    private int mSelectGuideId = -1;//选择的导购员编号
    //一码多品
    private View mSelectGoodsOfBarcodeView;
    private Dialog mSelectGoodsOfBarcodeDialog;
    private SelectGoodsAdapter mSelectGoodsAdapter;
    private String mBarcode;
    private String mProductCode;
    private String mMemberId = "";
    private String mChangePrice;
    private int mGoodsNum;
    private OpenOrderModel.CarItemsBean.TotalBean mTotal;
    private Bundle mBundle;

    public OpenOrderActivity() {
        super(R.layout.activity_open_order);
    }

    @Override
    protected void initView() {
        ViewGroup.LayoutParams params = mFlContent.getLayoutParams();
        params.width = (int) (ScreenUtil.getScreenWidthPix(this) * 0.85);
        mFlContent.setLayoutParams(params);
        replaceFragment(R.id.fl_content, new MemberInquireFragment(this));
        setSwipeMenuAndAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在栈顶就去请求
        getShoppingList();
    }

    /**
     * 设置适配器及侧滑删除按钮
     */
    private void setSwipeMenuAndAdapter() {
        mOpenOrderModelItemList = new ArrayList<>();
        mRvGoods.setLayoutManager(new LinearLayoutManager(this)); //设置布局样式
        mOpenOrderAdapter = new OpenOrderAdapter(mOpenOrderModelItemList);
        mRvGoods.setAdapter(mOpenOrderAdapter);

        //购物车商品的长按事件
        mOpenOrderAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                mOpenOrderAdapter.setPosition(position);
                mOpenOrderAdapter.setAdapterPosition(-1);
                mOpenOrderAdapter.setShow(true);
                return false;
            }
        });

        //滑动隐藏
        mRvGoods.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mOpenOrderAdapter.setPosition(-1);
                mOpenOrderAdapter.setAdapterPosition(-1);
                mOpenOrderAdapter.setShow(false);
            }
        });

        //适配器子item点击事件
        mOpenOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mOpenOrderAdapter.setPosition(-1);
                switch (view.getId()) {
//                    case R.id.tv_change_guide://修改导购员
//                        setChangeGuideDialog(position);
//                        break;
                    case R.id.tv_change_price://改价
                        setChangePriceDialog(mOpenOrderAdapter.getData().get
                                (position));
                        break;
                    case R.id.tv_change_num://改数量
                        mGoodsNum = mOpenOrderAdapter.getData().get(position)
                                .getNumber();
                        mOpenOrderAdapter.setAdapterPosition(position);
                        break;
                    case R.id.cb_goods_select://选择
                        mOpenOrderModelItemList.get(position).setChecked(mOpenOrderModelItemList.get(position)
                                .isChecked() ? false : true);
                        isAllChecked.clear();
                        for (OpenOrderModel.CarItemsBean.ItemsBean openOrderModel : mOpenOrderModelItemList) {
                            isAllChecked.add(openOrderModel.isChecked());
                        }
                        mCbAllChecked.setChecked(isAllChecked.contains(false) ? false : true);
                        break;
                    case R.id.iv_add://增加
                        mGoodsNum++;
                        mOpenOrderAdapter.getData().get(position).setGoodsNum
                                (mGoodsNum);
                        adapter.notifyDataSetChanged();
                        break;
                    case R.id.iv_reduce://减少
                        if (mGoodsNum > 0) {
                            mGoodsNum--;
                            mOpenOrderAdapter.getData().get(position)
                                    .setGoodsNum(mGoodsNum);
                            adapter.notifyDataSetChanged();
                        }
                        break;
                    case R.id.ll_goods_change://改价与改数量布局  点击消失
                        mOpenOrderAdapter.setPosition(-1);
                        break;
                    case R.id.tv_complete://完成
                        changeGoodsNum(mOpenOrderAdapter.getData().get(position)
                                .getProductCode(), mGoodsNum, mOpenOrderAdapter.getData().get(position).getIndex());
                        break;
                    case R.id.tv_cancel://取消
                        mGoodsNum = mOpenOrderAdapter.getData().get(position).getNumber();
                        mOpenOrderAdapter.getData().get(position).setGoodsNum(mGoodsNum);
                        mOpenOrderAdapter.setAdapterPosition(-1);
                        break;
                    case R.id.btn_del://删除
                        deleteGoodsItem(mOpenOrderAdapter.getData().get(position).getProductCode(), mOpenOrderAdapter
                                .getData().get(position).getIndex());
                        break;
                    case R.id.rl_content://item点击事件
                        mOpenOrderAdapter.setShow(false);
                        mOpenOrderAdapter.setAdapterPosition(-1);
                        break;
                }
            }
        });
    }


    @OnClick({R.id.iv_return, R.id.tv_member, R.id.tv_editor, R.id.ib_retail, R.id.iv_sure, R.id.iv_scan, R.id
            .iv_delete, R.id.tv_all_discounts, R.id.tv_goto_account, R.id.cb_all_checked, R.id.tv_delete})
// R.id.tv_guide
    void onClick(View v) {
        mOpenOrderAdapter.setPosition(-1);
        switch (v.getId()) {
            case R.id.iv_return://返回
                finish();
                break;
            case R.id.tv_member://会员
                mDlMenu.openDrawer(mFlContent);//打开侧滑菜单
                break;
            case R.id.tv_editor://编辑
                if (mOpenOrderAdapter != null) {
                    if (isEdit) {
                        setEdit(false);
                        mTvEditor.setText("编辑");
                        mRlAllChecked.setVisibility(View.GONE);
                        mLlBottom.setVisibility(View.VISIBLE);
                    } else {
                        setEdit(true);
                        mTvEditor.setText("完成");
                        mRlAllChecked.setVisibility(View.VISIBLE);
                        mLlBottom.setVisibility(View.GONE);
                        mCbAllChecked.setChecked(false);
                        for (OpenOrderModel.CarItemsBean.ItemsBean openOrderModel : mOpenOrderModelItemList) {
                            openOrderModel.setChecked(mCbAllChecked.isChecked());
                        }
                    }
                }
                break;
            case R.id.ib_retail://零售
                showShortToast("零售");
                break;
            case R.id.iv_sure://确认
                mBarcode = mEtBarcode.getText().toString();
                if (TextUtils.isEmpty(mBarcode)) {
                    showShortToast("请输入商品条码！");
                    return;
                }
                addGoodsToShopping();
                break;
            case R.id.iv_scan://扫描
                CustomCodeUtils.getInstance(this).startToCaptureActivity(this);
                break;
            case R.id.iv_delete://删除会员
                setDeleteMemberDialog();
                break;
            case R.id.tv_all_discounts://整单优惠
                if (mOpenOrderModelItemList.size() == 0) {
                    showShortToast("购物车为空,请添加商品！");
                    return;
                }
                mBundle = new Bundle();
                mBundle.putDouble("price", Double.parseDouble(mTvCollect.getText().toString()));
                startActivity(AllDiscountsActivity.class, mBundle);
                break;
            case R.id.tv_goto_account://结算
                if (mOpenOrderModelItemList.size() == 0) {
                    showShortToast("购物车为空,请添加商品！");
                    return;
                }
                gotoPayGoods();
                break;
            case R.id.cb_all_checked://全选
                for (OpenOrderModel.CarItemsBean.ItemsBean openOrderModel : mOpenOrderModelItemList) {
                    openOrderModel.setChecked(mCbAllChecked.isChecked());
                }
                break;
            case R.id.tv_delete://删除
                List<RemoveGoodsItemsModel> mRemoveGoodsItemsModelList = new ArrayList<>();
                for (int i = 0; i < mOpenOrderAdapter.getData().size(); i++) {
                    if (mOpenOrderAdapter.getData().get(i).isChecked()) {
                        mRemoveGoodsItemsModelList.add(new RemoveGoodsItemsModel(mOpenOrderAdapter.getData().get(i)
                                .getProductCode(), mOpenOrderAdapter.getData().get(i).getIndex()));
                    }
                }
                if (mRemoveGoodsItemsModelList.size() == 0) {
                    return;
                }
                deleteGoodsItems(GsonUtil.GsonString(mRemoveGoodsItemsModelList));
                break;
//            case R.id.tv_guide://导购员
//                showShortToast("导购员");
//                break;
        }
    }

    /**
     * 一码多品弹窗
     */
    private void selectGoodsOfBarcode(List<OpenOrderModel.DatasBean> data) {
        mSelectGoodsOfBarcodeView = View.inflate(this, R.layout.dialog_select_goods_of_barcode, null);
        mSelectGoodsOfBarcodeDialog = DialogUtil.getDialog(this, mSelectGoodsOfBarcodeView);
        RecyclerView rvSelectGoods = mSelectGoodsOfBarcodeView.findViewById(R.id.rv_select_goods);
        rvSelectGoods.setLayoutManager(new LinearLayoutManager(this));
        mSelectGoodsAdapter = new SelectGoodsAdapter(data);
        rvSelectGoods.setAdapter(mSelectGoodsAdapter);
        mProductCode = data.get(0).getProductCode();
        mSelectGoodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mSelectGoodsAdapter.setPosition(position);
                //获取选择的商品编码
                mProductCode = ((List<OpenOrderModel.DatasBean>) adapter.getData()).get(position).getProductCode();
            }
        });
        mSelectGoodsOfBarcodeView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //重新调用刷条码添加商品到购物车
                addGoodsToShopping();
                //商品编码置为空
                mProductCode = "";
                mSelectGoodsOfBarcodeDialog.dismiss();
            }
        });
        mSelectGoodsOfBarcodeView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mProductCode = "";
                mSelectGoodsOfBarcodeDialog.dismiss();
            }
        });

    }


    /**
     * 取消会员的弹窗
     */
    private void setDeleteMemberDialog() {
        mDeleteView = View.inflate(this, R.layout.dialog_delete_member, null);
        mDeleteDialog = DialogUtil.getDialog(this, true, mDeleteView, ScreenUtil
                .getScreenWidthPix(this) * 0.74);
        mDeleteView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMemberId = "";
                mSPUtils.put("member_id", mMemberId);
                mLlMember.setVisibility(View.GONE);
                mDeleteDialog.dismiss();
            }
        });
        mDeleteView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDeleteDialog.dismiss();
            }
        });
    }

    /**
     * 修改导购员弹窗
     */
    private void setChangeGuideDialog(final int i) {
        getGuideHttp();
        mChangeGuideView = View.inflate(OpenOrderActivity.this, R.layout.dialog_change_guide, null);
        mChangeGuideDialog = DialogUtil.getDialog(OpenOrderActivity.this, false, mChangeGuideView, ScreenUtil
                .getScreenWidthPix(this) * .85, ScreenUtil.getScreenHeightPix(this) * 0.65);
        RecyclerView rvChangeGuide = mChangeGuideView.findViewById(R.id.rv_change_guide);
        mChangeGuideView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mChangeGuideDialog.dismiss();
            }
        });
        mChangeGuideView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mSelectGuideId != -1) {
//                    mOpenOrderModelItemList.get(i).setGuideName(mGuideNameList.get(mSelectGuideId).getFullName());
                    mOpenOrderAdapter.notifyDataSetChanged();
                }
                mChangeGuideDialog.dismiss();
            }
        });
        rvChangeGuide.setLayoutManager(new LinearLayoutManager(this));
        mChangeGuideAdapter = new ChangeGuideAdapter(mGuideNameList);
        rvChangeGuide.setAdapter(mChangeGuideAdapter);
        mChangeGuideAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mChangeGuideAdapter.setPosition(position);
                mSelectGuideId = position;
            }
        });
    }

    /**
     * 改价弹窗
     */
    private void setChangePriceDialog(final OpenOrderModel.CarItemsBean.ItemsBean openOrderModel) {
        mChangePriceView = View.inflate(OpenOrderActivity.this, R.layout.dialog_change_price,
                null);
        mChangePriceDialog = DialogUtil.getDialog(OpenOrderActivity.this, true, mChangePriceView, ScreenUtil
                .getScreenWidthPix(this) * 0.9);
        ImageView ivGoodsPicture = mChangePriceView.findViewById(R.id.iv_goods_picture);
        TextView tvGoodsName = mChangePriceView.findViewById(R.id.tv_goods_name);
        TextView tvGoodsCode = mChangePriceView.findViewById(R.id.tv_goods_code);
        TextView tvGoodsPrice = mChangePriceView.findViewById(R.id.tv_goods_price);
        final EditText etChangePrice = mChangePriceView.findViewById(R.id.et_change_price);

        GlideUtil.loadImage(this, openOrderModel.getImageSmallUrl(), ivGoodsPicture);
        tvGoodsName.setText(openOrderModel.getTitle());
        tvGoodsCode.setText(openOrderModel.getBarcode());
        tvGoodsPrice.setText("原价：￥" + openOrderModel.getPreferentialPrice());
        mChangePriceView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mChangePriceDialog.dismiss();
            }
        });
        mChangePriceView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mChangePrice = etChangePrice.getText().toString();
                if (TextUtils.isEmpty(mChangePrice)) {
                    showShortToast("价格不能为空！");
                    return;
                }
                changeGoodsPrice(openOrderModel.getProductCode(), openOrderModel.getIndex());
            }
        });
    }

    /**
     * 刷条码添加商品到购物车
     */
    private void addGoodsToShopping() {
        mParams = new HashMap<>();
        mParams.put("barcode", mBarcode);
        mParams.put("productCode", mProductCode);
        mParams.put("memberId", mMemberId);
        mNetProtocol.addGoodsToShopping(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel data = (OpenOrderModel) baseBean.getData();
                mEtBarcode.setText("");
                if (data.getDatas().size() > 0) {
                    selectGoodsOfBarcode(data.getDatas());
                    return;
                }
                showShortToast("添加成功！");
                setAdapterData(data.getCarItems());
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 获取购物车列表
     */
    private void getShoppingList() {
        mNetProtocol.getShoppingList(this, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel.CarItemsBean data = (OpenOrderModel.CarItemsBean) baseBean.getData();
                setAdapterData(data);
            }

            @Override
            public void onFail(BaseBean baseBean) {

            }
        });
    }


    /**
     * 联网获取导购员
     */
    private void getGuideHttp() {
        mParams = new HashMap<>();
        mParams.put("pageIndex", "1");
        mParams.put("pageSize", "100");
        mNetProtocol.getGuide(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                GuideModel guideBeans = (GuideModel) baseBean.getData();
                mGuideNameList.clear();
                mGuideNameList.addAll(guideBeans.getDatas());
                if (mChangeGuideAdapter != null) {
                    mChangeGuideAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 修改购物车商品价格
     */
    private void changeGoodsPrice(String productCode, int index) {
        mParams = new HashMap<>();
        mParams.put("productCode", productCode);
        mParams.put("price", mChangePrice);
        mParams.put("index", index + "");
        mNetProtocol.changeGoodsPrice(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel.CarItemsBean data = (OpenOrderModel.CarItemsBean) baseBean.getData();
                setAdapterData(data);
                showShortToast("修改成功！");
                if (mChangePriceDialog != null) {
                    mChangePriceDialog.dismiss();
                }
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 修改购物车商品数量
     */
    private void changeGoodsNum(String productCode, int num, int index) {
        mParams = new HashMap<>();
        mParams.put("productCode", productCode);
        mParams.put("num", num + "");
        mParams.put("index", index + "");
        mNetProtocol.changeGoodsNum(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel.CarItemsBean data = (OpenOrderModel.CarItemsBean) baseBean.getData();
                setAdapterData(data);
                showShortToast("修改成功！");

            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 删除购物车商品
     */
    private void deleteGoodsItem(String productCode, int index) {
        mParams = new HashMap<>();
        mParams.put("productCode", productCode);
        mParams.put("index", index + "");
        mNetProtocol.deleteGoodsItem(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel.CarItemsBean data = (OpenOrderModel.CarItemsBean) baseBean.getData();
                setAdapterData(data);
                showShortToast("删除成功！");
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 批量删除购物车商品
     */
    private void deleteGoodsItems(String data) {
        mNetProtocol.deleteGoodsItems(this, data, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                OpenOrderModel.CarItemsBean data = (OpenOrderModel.CarItemsBean) baseBean.getData();
                mCbAllChecked.setChecked(false);
                setEdit(data.getItems().size() != 0 ? true : false);
                mTvEditor.setText(data.getItems().size() == 0 ? "编辑" : "完成");
                mRlAllChecked.setVisibility(data.getItems().size() == 0 ? View.GONE : View.VISIBLE);
                mLlBottom.setVisibility(data.getItems().size() != 0 ? View.GONE : View.VISIBLE);
                setAdapterData(data);
                showShortToast("删除成功！");
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 设置编辑
     */
    private void setEdit(boolean focus) {
        isEdit = focus;
        mOpenOrderAdapter.setChecked(focus);
        mTvMember.setClickable(!focus);
        mIbRetail.setClickable(!focus);
        mIvSure.setClickable(!focus);
        mIvScan.setClickable(!focus);
        mEtBarcode.setFocusable(!focus);
        if (!focus) {
            mEtBarcode.clearFocus();
        }
        mEtBarcode.setFocusableInTouchMode(!focus);
    }


    /**
     * 结算
     */
    private void gotoPayGoods() {
        mNetProtocol.gotoPayGoods(this, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mBundle = new Bundle();
                mBundle.putSerializable("data", baseBean);
                if (baseBean.getMessage().contains("促销")) {
                    mBundle.putSerializable("total", mTotal);
                }
                startActivity(PaymentActivity.class, mBundle);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 设置购物车清单数据
     *
     * @param data
     */
    private void setAdapterData(OpenOrderModel.CarItemsBean data) {
        mTotal = data.getTotal();
        mTvCollect.setText(DecimalFormatUtils.DecimalFormatTwo(data.getTotal().getAmount()));
        mTvDiscounts.setText(DecimalFormatUtils.DecimalFormatTwo(data.getTotal().getPreferential()));
        mTvNum.setText(data.getTotal().getCount() + "");
        mOpenOrderModelItemList.clear();
        mOpenOrderModelItemList.addAll(data.getItems());
        if (mOpenOrderAdapter != null) {
            mOpenOrderAdapter.setAdapterPosition(-1);
        }

        //购物车为空 不可编辑
        mTvEditor.setClickable(data.getItems().size() == 0 ? false : true);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CustomCodeUtils.getInstance(this).onCallBackListener(requestCode, data, this);
    }


    @Override
    public void getMemberInformation(Bundle bundle) {
        mLlMember.setVisibility(View.VISIBLE);
        MemberInquireModel memberInquireModel = (MemberInquireModel) bundle.getSerializable("member_information");
        mTvMemberName.setText(memberInquireModel.getRealName());
        mTvMemberPhone.setText(memberInquireModel.getMobilePhone());
        mDlMenu.closeDrawers();
        mMemberId = memberInquireModel.getMemberId();
        mSPUtils.put("member_id", mMemberId);
    }

    @Override
    public void onCallbackListener(String result) {
        mBarcode = result;
        addGoodsToShopping();
    }

    @Override
    public void onBackPressed() {
        if (mDlMenu.isDrawerOpen(mFlContent)) {
            mDlMenu.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }
}
